package org.rui.rpc.enums;

import cn.hutool.core.util.ObjUtil;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: linweirui
 * @date: 2024/12/09 9:22
 */
@Getter
public enum ProtocolMessageSerializerEnum {
    JDK(0,"jdk"),
    JSON(1,"json"),
    KRYO(2,"kryo"),
    HESSIAN(3,"hessian");

    private final int key;
    private final String text;
    ProtocolMessageSerializerEnum(int key, String text){
        this.key = key;
        this.text = text;
    }

    /**
     * 获取全部枚举类型的描述
     * @return
     */
    public static List<String> getValues(){
        return Arrays.stream(ProtocolMessageSerializerEnum.values())
                .map(p->p.text)
                .collect(Collectors.toList());
    }

    /**
     * 根据枚举值获取枚举类型
     * @param value
     * @return
     */
    public static ProtocolMessageSerializerEnum getMessageSerializerByValue(int value){
        for (ProtocolMessageSerializerEnum serializerEnum : ProtocolMessageSerializerEnum.values()) {
            if(serializerEnum.key == value){
                return serializerEnum;
            }
        }
        return null;
    }

    /**
     * 根据枚举名称获取对应枚举类型
     * @param text
     * @return
     */
    public static ProtocolMessageSerializerEnum getMessageSerializerByText(String text){
        if(ObjUtil.isEmpty(text)){
            return null;
        }
        for (ProtocolMessageSerializerEnum serializerEnum : ProtocolMessageSerializerEnum.values()) {
            String serializerName = serializerEnum.text;
            if(serializerName.equals(text)){
                return serializerEnum;
            }
        }
        return null;
    }
}
